package com.cloud.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.entity.ApplyBanner;
import com.cloud.system.service.ISysApplyBannerService;
import com.cloud.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 应用轮播图
 *
 * @author yzj
 * @since 2022-08-12
 */
@Tag(name = "轮播图管理")
@RestController
@RequestMapping("/system/applyBanner")
public class SysApplyBannerController extends BaseController {

    @Autowired
    private ISysApplyBannerService sysApplyBannerService;

    @Operation(summary = "应用轮播图分页")
    @RequestPermissions("system:applyBanner:list")
    @GetMapping("/list")
    public Result list(ApplyBanner applyBanner) {
        startPage();
        applyBanner.setSysCode(SecurityUtils.getSysCode());
        List<ApplyBanner> list = sysApplyBannerService.selectSysApplyBannerList(applyBanner);
        return getDataTable(list);
    }

    @Operation(summary = "新增应用轮播图信息")
    @Logs(title = "新增应用轮播图", businessType = BusinessType.INSERT)
    @RequestPermissions("system:applyBanner:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody ApplyBanner applyBanner) {
        applyBanner.setSysCode(SecurityUtils.getSysCode());
        return toResult(sysApplyBannerService.insertObject(applyBanner));
    }

    @Operation(summary = "修改应用轮播图信息")
    @Logs(title = "修改应用轮播图", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:applyBanner:edit")
    @PostMapping("/update")
    public Result update(@RequestBody ApplyBanner applyBanner) {
        applyBanner.setSysCode(SecurityUtils.getSysCode());
        return toResult(sysApplyBannerService.updateObject(applyBanner));
    }

    @Operation(summary = "查询应用轮播图详情")
    @RequestPermissions("system:applyBanner:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        return Result.success("查询成功", sysApplyBannerService.getObjectInfo(id));
    }

    @Operation(summary = "删除应用轮播图信息")
    @Logs(title = "删除应用轮播图", businessType = BusinessType.DELETE)
    @RequestPermissions("system:applyBanner:remove")
    @PostMapping("/remove")
    public Result remove(String ids) {
        List<String> list = Arrays.asList(ids.split(StringPool.COMMA));
        return toResult(sysApplyBannerService.removeByIds(list));
    }

    @Operation(summary = "刷新轮播图缓存")
    @RequestPermissions("system:applyBanner:remove")
    @GetMapping("/clearCache")
    public Result clearCache() {
        sysApplyBannerService.clearCache();
        return Result.success("操作成功");
    }

    @Operation(summary = "设置轮播图开启状态")
    @PostMapping("/setUpBanner")
    public Result setUpBanner(Long bannerId) {
        return toResult(sysApplyBannerService.setUpBanner(bannerId));
    }

    @Operation(summary = "设置轮播图关闭状态")
    @PostMapping("/closeUpBanner")
    public Result closeUpBanner(Long bannerId) {
        return toResult(sysApplyBannerService.closeUpBanner(bannerId));
    }

    @Operation(summary = "移动端查询轮播图列表")
    @GetMapping("/allList")
    public Result allList() {
        return Result.success("查询成功", sysApplyBannerService.getAllApplyBannerList());
    }

}
